package net.yambal.air.trackManager.GPX
{
	import com.google.maps.LatLng;
	import com.google.maps.LatLngBounds;
	import com.google.maps.overlays.Polyline;
	import com.google.maps.overlays.PolylineOptions;
	
	import mx.collections.ArrayCollection;

    /**
    * GPX.TRK.TRKSEG もしくは GPX.RTE
    * GPX.TRK.TRKSEG.TRKPT もしくは GPX.RTE.RTEPT のリストが格納された ArrayCollection
    **/
	public class WayPointArrayCollection extends ArrayCollection{
        
		public static const LIST_TYPE_TRK:String = 'trk';
        public static const LIST_TYPE_RTE:String = 'rte';
        
        
        
        public var name:String;
        public var desc:String;
        public var number:Number;

        public var SouthLat:Number = 999;
		public var WestLon:Number = 999;
		public var NorthLat:Number = -999;
		public var EastLon:Number = -999;
        
        public var _gPolyline:Polyline;
        
        // このリストが　Trk, Rte の何に由来するか
        private var _listType:String;
		
		public function WayPointArrayCollection(RteOrSegment:XML, type:String){
            this._listType = type;
            
            RteOrSegment.ignoreWhitespace = true;
			var ns:Namespace = RteOrSegment.namespace();
            
            this.name = RteOrSegment.name;
            this.desc = RteOrSegment.desc;

            var wptList:XMLList;
            if(type == LIST_TYPE_TRK){
                wptList = RteOrSegment.ns::trkpt;
            }else{
                wptList = RteOrSegment.ns::rtept;
                this.number = RteOrSegment.number;
            }

			for(var i:Number = 0; i < wptList.length(); i++){
                var pType:String = WayPoint.POINT_TYPE_WPT;
                if(type == LIST_TYPE_RTE){
                    pType = WayPoint.POINT_TYPE_RTE;
                }else if(type == LIST_TYPE_TRK){
                    pType = WayPoint.POINT_TYPE_TRK;
                }
				var wpt:WayPoint = new WayPoint(wptList[i] as XML, pType);
                this.addItem(wpt);
				
                if(wpt.lat < this.SouthLat){
                    this.SouthLat = wpt.lat;
                }
                if(wpt.lat > this.NorthLat){
                    this.NorthLat = wpt.lat;
                }
                if(wpt.lon < this.WestLon){
                    this.WestLon = wpt.lon;
                }
                if(wpt.lon > this.EastLon){
                    this.EastLon = wpt.lon;
                }
			}
 
		}
        
        public function get gPolyline():Polyline{
            if(this._gPolyline){
                return this._gPolyline;
            }
            
            var rutePolylinePoints:Array = new Array();
            for each (var rutePoint:WayPoint in this){
                var rutell:LatLng = new LatLng(rutePoint.lat, rutePoint.lon);
                rutePolylinePoints.push(rutell);
            }
            
            this._gPolyline = new Polyline(rutePolylinePoints,
                new PolylineOptions({
                    strokeStyle: {
                        thickness: 3,
                        color: 0x00FF00,
                        alpha: 0.75,
                        pixelHinting: true
                    }
                }));
            return this._gPolyline;
        }
        
        public function get bounds():LatLngBounds{
			var southWest:LatLng = new LatLng(this.SouthLat, this.WestLon);
			var northEast:LatLng = new LatLng(this.NorthLat, this.EastLon);
			return new LatLngBounds(southWest,northEast);
        }
		
		public function get center():LatLng{
			return new LatLng((this.SouthLat + this.NorthLat) / 2, (this.WestLon + this.EastLon) / 2);
		}
	}
}